package com.platform.service.impl;

import com.platform.dao.*;
import com.platform.entity.*;
import com.platform.utils.BeanUtils;
import com.platform.utils.RRException;
import com.platform.utils.ShiroUtils;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.platform.service.ProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-30 14:31:21
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private GoodsSpecificationDao goodsSpecificationDao;
    @Autowired
    private CartDao cartDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ProductEntity queryObject(Integer id) {
        return productDao.queryObject(id);
    }

    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        List<ProductEntity> list = productDao.queryList(map);

        List<ProductEntity> result = new ArrayList<>();
        //翻译产品规格
        if (null != list && list.size() > 0) {
            for (ProductEntity item : list) {
                String specificationIds = item.getGoodsSpecificationIds();
                String specificationValue = "";
                if (!StringUtils.isNullOrEmpty(specificationIds)) {
                    String[] arr = specificationIds.split("_");

                    for (String goodsSpecificationId : arr) {
                        GoodsSpecificationEntity entity = goodsSpecificationDao.queryObject(goodsSpecificationId);
                        if (null != entity) {
                            specificationValue += entity.getValue() + "；";
                        }
                    }
                }
                item.setSpecificationValue(item.getGoodsName() + " " + specificationValue);
                result.add(item);
            }
        }
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return productDao.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(ProductEntity product) {
//        int result = 0;
//        String goodsSpecificationIds = product.getGoodsSpecificationIds();
//        if (!StringUtils.isNullOrEmpty(goodsSpecificationIds)) {
//            if (goodsSpecificationIds.contains("_")) {
//                String[] goodsSpecificationIdArr = goodsSpecificationIds.split("_");
//                for (int i = 0; i < goodsSpecificationIdArr.length - 1; i++) {
//                    String[] oneId = goodsSpecificationIdArr[i].split(",");
//                    String[] twoId = goodsSpecificationIdArr[i + 1].split(",");
//                    for (int j = 0; j < oneId.length; j++) {
//                        for (int k = 0; k < twoId.length; k++) {
//                            String strGoodsSpecificationIds = null;
//                            if (StringUtils.isNullOrEmpty(oneId[j]) || StringUtils.isNullOrEmpty(twoId[k])) {
//                                continue;
//                            }
//                            strGoodsSpecificationIds = oneId[j] + "_" + twoId[k];
//                            product.setGoodsSpecificationIds(strGoodsSpecificationIds);
//                            ProductEntity entity = new ProductEntity();
//                            BeanUtils.copyProperties(product, entity);
//                            result += productDao.save(entity);
//                        }
//                    }
//                }
//            } else {
//                if (goodsSpecificationIds.contains(",")) {
//                    String[] split = goodsSpecificationIds.split(",");
//                    for (String goodsSpecificationId : split) {
//                        product.setGoodsSpecificationIds(goodsSpecificationId);
//                        ProductEntity entity = new ProductEntity();
//                        BeanUtils.copyProperties(product, entity);
//                        result += productDao.save(entity);
//                    }
//                } else {
//                    product.setGoodsSpecificationIds(goodsSpecificationIds);
//                    ProductEntity entity = new ProductEntity();
//                    BeanUtils.copyProperties(product, entity);
//                    result += productDao.save(entity);
//                }
//
//            }
//
//        }
        return productDao.save(product);
    }

    @Override
    public int update(ProductEntity product) {
//        if (StringUtils.isNullOrEmpty(product.getGoodsSpecificationIds())) {
//            product.setGoodsSpecificationIds("");
//        }
        //修改购物车里面的产品名称
        Map<String, Object> params = new HashMap<>();
        params.put("productId", product.getId());
        List<CartEntity> cartEntities = cartDao.queryList(params);

        if (cartEntities.size() > 0) {
            for (CartEntity cartEntity : cartEntities) {
                cartEntity.setProductName(product.getProductName());
                cartEntity.setProductPicUrl(product.getPrimaryPicUrl());
                cartEntity.setGoodsSn(product.getGoodsSn());
                cartEntity.setMarketPrice(product.getMarketPrice());
                cartEntity.setRetailPrice(product.getRetailPrice());
                cartDao.update(cartEntity);
            }
        }

        //查看是否商品里面的主产品，是的话同步修改
        List<GoodsEntity> goodsEntities = goodsDao.queryList(params);
        if (goodsEntities.size() > 0) {
            for (GoodsEntity goodsEntity : goodsEntities) {
                goodsEntity.setGoodsSn(product.getGoodsSn());
                goodsEntity.setMarketPrice(product.getMarketPrice());
                goodsEntity.setRetailPrice(product.getRetailPrice());
                goodsEntity.setGoodsNumber(product.getGoodsNumber());
                goodsDao.update(goodsEntity);
            }
        }

        return productDao.update(product);
    }

    @Override
    public int delete(Integer id) {
        return productDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {

        return productDao.deleteBatch(ids);
    }

    @Override
    public int enSale(Integer id) {
        ProductEntity productEntity = queryObject(id);
        if (1 == productEntity.getIsOnSale()) {
            throw new RRException("此产品已处于上架状态！");
        }
        productEntity.setIsOnSale(1);
        return productDao.update(productEntity);
    }

    @Override
    public int unSale(Integer id) {
        ProductEntity productEntity = queryObject(id);
        if (0 == productEntity.getIsOnSale()) {
            throw new RRException("此产品已处于下架状态！");
        }
        productEntity.setIsOnSale(0);
        return productDao.update(productEntity);
    }
}
